package net.wang.concurrency.test.singleton;


import net.wang.concurrency.annoations.Recommend;
import net.wang.concurrency.annoations.ThreadSafe;

/**
 * 单例模式——枚举
 */
@ThreadSafe
public class SingletonExample7 {

    private SingletonExample7() {

    }

    /**
     * 线程是安全
     *
     * @return
     */
    @Recommend
    public static SingletonExample7 getInstance() {
        return SingletonEnum.INSTANCE.getInstance();
    }


    private enum SingletonEnum {
        INSTANCE;
        private SingletonExample7 instance;

        /**
         * 枚举类JVM此方法只会调用一次
         */
        SingletonEnum() {
            instance = new SingletonExample7();
        }

        public SingletonExample7 getInstance() {
            return instance;
        }
    }
}
